package com.example.demo.controller;

import com.example.demo.model.LinkCategory;
import com.example.demo.service.LinkCategoryService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/dashboard/link-categories")
public class LinkCategoryController {
    
    private final LinkCategoryService linkCategoryService;
    private final UserService userService;
    
    @Autowired
    public LinkCategoryController(LinkCategoryService linkCategoryService, UserService userService) {
        this.linkCategoryService = linkCategoryService;
        this.userService = userService;
    }
    
    @GetMapping
    public String listCategories(Model model) {
        try {
            List<LinkCategory> categories = linkCategoryService.findAllLinkCategories();
            model.addAttribute("categories", categories);
            model.addAttribute("category", new LinkCategory());
            model.addAttribute("user", userService.getCurrentUser());
            return "dashboard/link-categories/list";
        } catch (Exception e) {
            model.addAttribute("errorMessage", "获取分类列表失败: " + e.getMessage());
            model.addAttribute("categories", null);
            model.addAttribute("category", new LinkCategory());
            model.addAttribute("user", userService.getCurrentUser());
            return "dashboard/link-categories/list";
        }
    }
    
    @GetMapping("/new")
    public String newCategoryForm(Model model) {
        model.addAttribute("category", new LinkCategory());
        model.addAttribute("user", userService.getCurrentUser());
        return "dashboard/link-categories/form";
    }
    
    @PostMapping
    public String saveCategory(@ModelAttribute LinkCategory category, RedirectAttributes redirectAttributes) {
        try {
            linkCategoryService.saveLinkCategory(category);
            redirectAttributes.addFlashAttribute("successMessage", 
                category.getId() == null ? "网站链接分类创建成功!" : "网站链接分类更新成功!");
            return "redirect:/dashboard/link-categories";
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", "保存网站链接分类失败: " + e.getMessage());
            return "redirect:/dashboard/link-categories";
        }
    }
    
    @GetMapping("/edit/{id}")
    public String editCategoryForm(@PathVariable Long id, Model model, RedirectAttributes redirectAttributes) {
        try {
            Optional<LinkCategory> categoryOpt = linkCategoryService.findById(id);
            if (categoryOpt.isPresent()) {
                model.addAttribute("category", categoryOpt.get());
                model.addAttribute("user", userService.getCurrentUser());
                return "dashboard/link-categories/form";
            } else {
                redirectAttributes.addFlashAttribute("errorMessage", "未找到ID为 " + id + " 的网站链接分类");
                return "redirect:/dashboard/link-categories";
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", "编辑网站链接分类失败: " + e.getMessage());
            return "redirect:/dashboard/link-categories";
        }
    }
    
    @GetMapping("/delete/{id}")
    public String deleteCategory(@PathVariable Long id, RedirectAttributes redirectAttributes) {
        try {
            linkCategoryService.deleteLinkCategory(id);
            redirectAttributes.addFlashAttribute("successMessage", "网站链接分类删除成功!");
            return "redirect:/dashboard/link-categories";
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", "删除网站链接分类失败: " + e.getMessage());
            return "redirect:/dashboard/link-categories";
        }
    }
} 